#include "CModel/CModel.h"
#include <ctime>
#include <algorithm>
#include <QDebug>

CModel::CModel(QAbstractItemModel *parent) : QAbstractItemModel(parent)
{
    for (int i=0; i<10; ++i) {
        m_stocks.append({i, i*100, rand() % 100, rand() % 300, i, i});
        _colors.append(QColor(Qt::black));
    }
}

void CModel::setRowColor(const int &row, const QString &color)
{
    QColor txtColor;
    if (color == "red") {
        txtColor = Qt::red;
    } else if (color == "green") {
        txtColor = Qt::green;
    } else {
        txtColor = Qt::black;
    }
    _colors[row] = txtColor;
    auto start = this->index(row, 0);
    auto end = this->index(row, this->columnCount(QModelIndex()) - 1);
    emit dataChanged(start, end);
}

int CModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_stocks.length();
}
int CModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_names.length();
}

QVariant CModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid()){
        qDebug() << "index is valid: " << index.row() << ":" << index.column();
        return QVariant();
    }
    if(index.row() > m_stocks.length() || index.column() > m_names.length()){
        qDebug() << "m_stocks len: " << m_stocks.length() << " " << index.row() << ":" << index.column();
        return QVariant();
    }
    if(role == Qt::DisplayRole){
        auto stock = m_stocks.at(index.row());
        return (index.column() > stock.length() || index.column() < 0) ? QVariant() : stock.at(index.column());
    } else if(role == Qt::EditRole){
        auto stock = m_stocks.at(index.row());
        return (index.column() > stock.length() || index.column() < 0) ? QVariant() : stock.at(index.column());
    } else if (role == Qt::TextColorRole) {
        return QVariant(_colors.at(index.row()));
    } else if (role == Qt::TextAlignmentRole) {
        return QVariant(Qt::AlignCenter|Qt::AlignVCenter);
    }
    return QVariant();
}

bool CModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(!index.isValid()){
        qDebug() << "index is valid: " << index.row() << ":" << index.column();
        return false;
    }
    if(role == Qt::EditRole){
        auto stock = m_stocks.at(index.row());
        stock[index.column()] = value;
        emit dataChanged(index, index);
        return true;
    }
    return false;
}

QVariant CModel::headerData(int section, Qt::Orientation orientation, int role) const
{
//    if(role != Qt::DisplayRole){
//        return QVariant();
//    }

//    if(orientation == Qt::Horizontal){
//        if(section < 0 || section > m_names.length()){return  QVariant();}
//        return QVariant(m_names.at(section));
//    }else {
//        return QVariant(QString::number(section));
//    }
    if(orientation == Qt::Horizontal){
        if (role == Qt::DisplayRole) {
            return QVariant(m_names.at(section));
        } else {
           qDebug() << role;
        }
    } else if (orientation == Qt::Vertical) {
        if (role == Qt::DisplayRole) {
            return QVariant(QString::number(section));
        }
    }
    return QVariant();
}
bool CModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
    if(role != Qt::EditRole){
        return false;
    }
    if(orientation == Qt::Horizontal){
        m_names.replace(section, value.toString());
        emit headerDataChanged(Qt::Horizontal, section, section);
        return true;
    }else{
        return false;
    }
}

Qt::ItemFlags CModel::flags(const QModelIndex &index) const
{
//    if(index.isValid()){
//         Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren | Qt::ItemIsEditable;
//         return flags;
//    }
//    return QAbstractItemModel::flags(index);
    if(index.isValid()){
        Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable
                | Qt::ItemNeverHasChildren | Qt::ItemIsEditable;
        return flags | QAbstractItemModel::flags(index);
    }
    return QAbstractItemModel::flags(index);
}

QModelIndex CModel::index(int row, int column, const QModelIndex &parent) const
{
    if (row < 0 || column < 0 || column >= columnCount(parent) || column > m_names.length())
        return QModelIndex();

    return createIndex(row,column);
}

QModelIndex CModel::parent(const QModelIndex &child) const
{
    Q_UNUSED(child);
    return QModelIndex();
}
